package com.zbcbms.controller;


import com.alibaba.druid.util.StringUtils;
import com.zbcbms.common.AlertUtilCommon;
import com.zbcbms.entity.Book;
import com.zbcbms.service.impl.BookServiceImpl;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Alert;
import javafx.scene.control.DatePicker;
import javafx.scene.control.TextField;
import java.math.BigDecimal;
import java.net.URL;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.ResourceBundle;

/**
 * @author fengq
 */
public class AddBookController implements Initializable {
    @FXML
    private TextField addBookNumber;
    @FXML
    private TextField addBookName;
    @FXML
    private TextField addBookAuthor;
    @FXML
    private TextField addPublishingHouse;
    @FXML
    private DatePicker addPublishingTime;
    @FXML
    private TextField addPrice;
    @FXML
    private TextField addType;

    @Override
    public void initialize(URL location, ResourceBundle resources) {

    }

    @FXML
    public void addBookAction(){
        String bookNumber = addBookNumber.getText();
        String bookName = addBookName.getText();
        String bookAuthor = addBookAuthor.getText();
        String publishingHouse = addPublishingHouse.getText();
        String publishingTime = addPublishingTime.getEditor().getText();
        String type = addType.getText();
        String price = addPrice.getText();
        if (StringUtils.isEmpty(bookNumber)||StringUtils.isEmpty(bookName)||StringUtils.isEmpty(price)||StringUtils.isEmpty(type)||StringUtils.isEmpty(bookAuthor)){
            new AlertUtilCommon().AlterMessageInform(addBookNumber.getScene().getWindow(), Alert.AlertType.ERROR,"错误提醒","关键参数不能空","修改失败");
        }else {
            if (StringUtils.isEmpty(publishingHouse)||publishingHouse==""){
                publishingHouse="null";
            }
            if (StringUtils.isEmpty(publishingTime)||publishingTime==""){
                publishingTime="null";
            }

            try {
                Book book = new Book();
                book.setBookNumber(bookNumber);
                book.setBookName(bookName);
                book.setAuthor(bookAuthor);
                book.setPublishingHouse(publishingHouse);
                Date date = new SimpleDateFormat("yyyy-MM-dd").parse(publishingTime);
                book.setPublishingTime(date);
                book.setPrice(new BigDecimal(price));
                book.setType(type);
                Boolean aBoolean = new BookServiceImpl().saveOne(book);
                if (aBoolean){
                    new AlertUtilCommon().AlterMessageInform(addBookNumber.getScene().getWindow(), Alert.AlertType.INFORMATION,"添加提醒","添加成功","小仲书: 太好了，又来新伙伴了");
                    resetAction();
                }else {
                    new AlertUtilCommon().AlterMessageInform(addBookNumber.getScene().getWindow(), Alert.AlertType.INFORMATION,"添加提醒","添加错误","小仲书提醒你:差忆点点成功");
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
    }

    @FXML
    public void resetAction(){
        addBookNumber.setText("");
        addBookName.setText("");
        addBookAuthor.setText("");
        addPublishingHouse.setText("");
        addPublishingTime.getEditor().setText("");
        addPrice.setText("");
        addType.setText("");
    }

}
